package com.ruoyi.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.exam.domain.Subject;
import com.ruoyi.exam.mapper.SubjectMapper;
import com.ruoyi.exam.service.ISubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.exam.viewmodel.admin.education.SubjectPageRequestVM;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  学科表 服务实现类
 * </p>
 *
 * @author JY
 * @since 2024-02-23
 */
@Service("SubjectServiceImpl")
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public List<Subject> getSubjectByLevel(Integer level) {

        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("level", level)
                .orderByAsc("item_order");

        return subjectMapper.selectList(wrapper);
    }

    @Override
    public Integer levelBySubjectId(Integer id) {

        return subjectMapper.selectById(id).getLevel();
    }

    @Override
    public Page<Subject> page(SubjectPageRequestVM requestVM) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0);
        if(requestVM.getId() != null && requestVM.getId() != 0){
            wrapper.eq("id", requestVM.getId());
        }
        if(requestVM.getLevel() != null && requestVM.getLevel() != 0){
            wrapper.eq("level", requestVM.getLevel());
        }

        return subjectMapper.selectPage(new Page<>(requestVM.getPageNum(), requestVM.getPageSize()), wrapper);
    }
}
